package com.gzdh.dishrecognition.ui.fragment.sysinfo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.greendao.dao.BmcmEntityDao;
import com.greendao.dao.MacMainpSbruanDinnerEntityDao;
import com.gzdh.dishrecognition.AppContext;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.MacMainpSbruanDinnerEntity;
import com.gzdh.dishrecognition.ui.adapter.BmcmInfoAdapter;
import com.gzdh.dishrecognition.ui.dialog.SetBmcmDialog;
import com.gzdh.dishrecognition.ui.widget.SwipeLayoutManager;
import com.gzdh.dishrecognition.util.ProductSUtils;

import java.util.List;

public class BmcmInfoFragment extends Fragment {


    private Context mContext;

    private Activity activity;

    private BmcmEntityDao bmcmEntityDao;

    private BmcmInfoAdapter bmcmInfoAdapter;

    private RecyclerView recyclerView;

    private SetBmcmDialog setBmcmDialog;

    private MacMainpSbruanDinnerEntityDao macMainpSbruanDinnerEntityDao;

    public BmcmInfoFragment(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_bmcm_info, container, false);
        mContext = view.getContext();
        bmcmEntityDao = AppContext.getInstance().getDaoSession().getBmcmEntityDao();
        macMainpSbruanDinnerEntityDao = AppContext.getInstance().getDaoSession().getMacMainpSbruanDinnerEntityDao();
        recyclerView = view.findViewById(R.id.recyclerView);
        bmcmInfoAdapter = new BmcmInfoAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(bmcmInfoAdapter);


        setBmcmDialog = new SetBmcmDialog(mContext);
        view.findViewById(R.id.addBut).setOnClickListener(view1 -> {
            setBmcmDialog.show();
        });
        setBmcmDialog.setResListener(() -> {
            show();
        });
        show();
        return view;
    }


    private void show() {
        List<MacMainpSbruanDinnerEntity> macMainpSbruanDinnerList = macMainpSbruanDinnerEntityDao.loadAll();
        ProductSUtils.setSbruanDinnerName(macMainpSbruanDinnerList);
        bmcmInfoAdapter.setList(macMainpSbruanDinnerList);
    }
    @Override
    public void onPause() {
        SwipeLayoutManager.getInstance().clearCurrentLayout();
        super.onPause();
    }
}